"use strict";
advertiserApp.controller("advertiserCtrl",function($scope,$http){
	 $http.post("/web-adv/finishPlacementAd").success(function(data){
		 $scope.tableData = data.beans;	
	 });

	 //时间格式转换
	 $scope.translate = function(t){
		 //t = t.replace(/-/g,'/');
		 //t = t.split(".")[0];		 
		 var date = new Date(t);		 
		 return date.getTime();
	 };
	 /**
	  * @title  删除已投放广告（伪删除）
	  * @author WangHan
	  * @email 774576529@qq.com
	  * @time 2017/02
	  * @version 1.0
	  */
	 $scope.deletePlacementAd = function(id,advertiserBalance,adId,adBalance){
		 if(advertiserBalance<0){
			 var advertiserBalance = 0.0;
		 }
		 var add=advertiserBalance*1+adBalance;
		 var msg = "您这条广告的余额为"+add+"，您确定要删除么！";
			if (confirm(msg)){			
				$.ajax({
					url : '/web-adv/deletePlacementAd',
			        data:{id:id,adId:adId,add:add},
					type : 'post',
					success : function(data) {			
						alert(data.data);
						location.href ="/web-adv/advWeb/views/adManagement/alreadyAd/index.html?selectedNav%3D2%26navPos%3D0";
					}
				});
			};		 
	 };
	 /**
		 * 现在时间格式转换
		 */
		function show(){
		   var mydate = new Date();
		   var mounth;
		   var day;
		   if(mydate.getMonth()+1<10){
			   mounth = "0"+(mydate.getMonth()+1);
		   }else{
			   mounth = mydate.getMonth()+1;
		   }
		   if(mydate.getDate()<10){
			   day = "0"+mydate.getDate();
		   }else{
			   day = mydate.getDate();
		   }
		   var str = "" + mydate.getFullYear() + "-";
		   str += mounth + "-";
		   str += day;
		   return str;
		  }
	 /**
	  *暂停广告 
	  */
	 $scope.stop = function(adId){
		var msg = "您确定要暂停此条广告吗？";
		if (confirm(msg)){			
			$.ajax({
				url : '/web-adv/updatePlacementAdStatus',
		        data:{adId:adId,check:'1'},
				type : 'post',
				success : function(data) {			
					alert(data.data);
					location.href ="/web-adv/advWeb/views/adManagement/alreadyAd/index.html?selectedNav%3D2%26navPos%3D0";
				}
			});
		};
	 }
	 /**
	  *投放广告 
	  */
	 $scope.putIn = function(id,adType,adId,adStatus){
		var msg = "您确定要投放此条广告吗？";
		if (confirm(msg)){
			if(adType=="picture"&&adStatus=="系统下架(余额)"){
				alert("余额不足，请充值！")
				location.href ="/web-adv/advWeb/views/adManagement/pictureAd/editPictureAd.html?selectedNav=2&navPos=0&adId="+adId;
				return false;
			}
			if(adType=="video"&&adStatus=="系统下架(余额)"){
				alert("余额不足，请充值！")
				location.href ="/web-adv/advWeb/views/adManagement/vedioAd/editVedioAd.html?selectedNav=2&navPos=0&adId="+adId;
				return false;
			}
			$.ajax({
				url : '/web-adv/updatePlacementAdStatus',
		        data:{adId:adId,check:'0'},
				type : 'post',
				success : function(data) {
					alert(data.data);
					location.href ="/web-adv/advWeb/views/adManagement/alreadyAd/index.html?selectedNav%3D2%26navPos%3D0";
				}
			});
		};
	 }
	 $scope.details = function(value){
		 if(value=="video") return true;
		 return false;		 
	 }
	 $scope.editStatus = function(value){
		 if(value=="投放中") return true;
		 return false;		 
	 }
	 $scope.editStop = function(value){
		 if(value=="手动下架"||value=="系统下架(时间)"||value=="系统下架(余额)"||value=="系统下架(时间、余额)") return true;
		 return false;		 
	 }
	 $scope.editSystem = function(value){
		 if(value=="系统下架") return true;
		 return false;		 
	 }
	 $scope.padVersion = function(value){
		 if(value=="pad") return true;
		 return false;		 
	 }
	 $scope.pcVersion = function(value){
		 if(value=="pc") return true;
		 return false;		 
	 }
	 $scope.mobileVersion = function(value){
		 if(value=="mobile") return true;
		 return false;		 
	 }
	 $scope.tvVersion = function(value){
		 if(value=="tv") return true;
		 return false;		 
	 }
	 $scope.lineStatus = function(value){
		 if(value=="可投放") return true;
		 return false;		 
	 }
	 $scope.lnStatus = function(value){
		 if(value=="不可投放") return true;
		 return false;		 
	 }
	 $scope.adBalance = function(balance){
		 if(balance=="系统下架(余额)"||balance=="系统下架(时间、余额)") return true;
		 return false;		 
	 }
	 $scope.adNBalance = function(balance,time){
		 var nowTime = show();
		 var getValue=time.split(" ")[0];
		 if(balance=="系统下架(余额)"&&getValue <nowTime ) return true;
		 return false;		 
	 }
	 $scope.endTime = function(value){
		 var nowTime = show();
		 var getValue=value.split(" ")[0];
		 if(getValue<nowTime) return true;
		 return false;		 
	 }
	 $scope.endNTime = function(value){
		 var nowTime = show();
		 var getValue=value.split(" ")[0];
		 if(getValue>=nowTime) return true;
		 return false;		 
	 }
	 
	var vedioSaveUrl = "http://114.116.41.171:8089/videos/";//服务器保存视频的路径
	var pictureSaveUrl = "http://114.116.41.171:8089/pictures/";//服务器保存图片的路径
//	var vedioSaveUrl = "http://192.168.3.67:8089/videos/";//服务器保存视频的路径
//	var pictureSaveUrl = "http://192.168.3.67:8089/pictures/";//服务器保存图片的路径
	 $scope.flashVideo = function(vedioUrl,vedioPage){
			 var flashvars={
	             f:vedioSaveUrl+vedioUrl,//视频url
	             c:0,
	             b:1, 
	             i:pictureSaveUrl+vedioPage, //视频封面url
			     my_url:window.location.href  //分享地址
	         };
	         var params={bgcolor:'#FFF',allowFullScreen:true,allowScriptAccess:'always',wmode:'transparent'};
	         CKobject.embedSWF('/web-adv/advWeb/static/vendor/ckplayer/ckplayer.swf','flashVideoBox','ckplayer_a1','100%','100%',flashvars,params);
	         flashImg.closeVideo=function(ele){				     
		     ele.siblings("div#flashVideoBox").empty();									 
	     };		 
	 }
	 $scope.flashImg = function(pictureUrl,picturePage){
		 $("#pictureAdImg").prop("src",pictureSaveUrl+picturePage);
		 $("#pictureUl").children().remove();
		 var beans = pictureUrl.split("|");
		 var li="";
		 for(var i=0;i<beans.length;i++){
			 if(beans[i]==""||beans[i]==null){
				 beans.splice([i],1);
			 }
			 
			 li+= '<li><img src="'+pictureSaveUrl+beans[i]+'" alt="" /></li>';
		 }
		 var imgNum=3-beans.length%3;
		 if(beans.length<3){
			 for(var j=0;j<imgNum;j++){
					li+="<li>"+
					         "<img src='' alt='' index='"+j+"'/>"+
					    "</li>";
			 }
		 }
		 $("#pictureUl").append(li);
		 var ulInitLeft=$(".adCoverFilmsWrap li").length/3*1035;  //初始向左便宜一套ul的长度
		 $("#pictureUl").css("left",-ulInitLeft+"px");
		 $("#pictureUl").append(li);
         $("#pictureUl").append(li);
	}
});


